/**
 * 定义表单itfreerForm.core模块
 */
(function(window, angular, undefined){
	/**
	* 定义WDP模块
	*/
	var wdp = angular.module('water.apps.sw.wdp');
	
	/**
	* 路由配置
	*/
	wdp.config(function ($stateProvider, $urlRouterProvider) {
		
		var sessionId = $("#_sessionid").html();

        /**
         * 首页
         */
        $stateProvider.state('homePage',{
            views: {
                'panel': {
                    controller : 'homePageController',
                    templateUrl: getContextPath() + '/webResource/view/sw/homePage/main.html?sessionId='+sessionId
                }
            }
        });

        /**
         * 运行信息
         */
        $stateProvider.state('operInfo',{
            views: {
                'panel': {
                    controller : 'operInfoController',
                    templateUrl: getContextPath() + '/webResource/view/sw/operInfo/main.html?sessionId='+sessionId
                }
            }
        });
		
		/**
		 * 配置存储
		 */
		$stateProvider.state('dataMaintain',{
			views: {
				'panel': {
					controller : 'dataMaintainManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/dataMaintain/main.html?sessionId='+sessionId
				}
			}
		});

        /**
         *  校准设置
         */
        $stateProvider.state('calSetting',{
            views: {
                'panel': {
                    controller : 'calSettingManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/calSetting/main.html?sessionId='+sessionId
                }
            }
        });
		
		/**
		 * 站点信息
		 */
		$stateProvider.state('bcSite',{
			views: {
				'panel': {
					controller : 'bcSiteManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcSite/main.html?sessionId='+sessionId
				}
			}
		});
		
		/**
		 * 串口配置
		 */
		$stateProvider.state('bcSerial',{
			views: {
				'panel': {
					controller : 'bcSerialManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcSerial/main.html?sessionId='+sessionId
				}
			}
		});
		
		/**
		 * 采集模块配置
		 */
		$stateProvider.state('bcDevice',{
			views: {
				'panel': {
					controller : 'bcDeviceManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcDevice/main.html?sessionId='+sessionId
				}
			}
		});
		
		/**
		 * 监测参数字典管理
		 */
		$stateProvider.state('bcMonitorParamDict',{
			views: {
				'panel': {
					controller : 'bcMonitorParamDictManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcMonitorParamDict/main.html?sessionId='+sessionId
				}
			}
		});
		
		/**
		 * 仪器监测参数关联管理
		 */
		$stateProvider.state('bcDeviceMonitorParam',{
			views: {
				'panel': {
					controller : 'bcDeviceMonitorParamManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcDeviceMonitorParam/main.html?sessionId='+sessionId
				}
			},
			params:{'deviceId':"2"}
		});
		
		/**
		 * 仪器监测参数关联管理
		 */
		$stateProvider.state('bcDeviceConfig',{
			views: {
				'panel': {
					controller : 'bcDeviceConfigManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/bcDeviceConfig/main.html?sessionId='+sessionId
				}
			},
			params:{'deviceId':"2"}
		});
		
		/**
		 * 数据监控
		 */
		$stateProvider.state('monitorData',{
			views: {
				'panel': {
					controller : 'monitorDataManagerController',
					templateUrl: getContextPath() + '/webResource/view/sw/monitorData/main.html?sessionId='+sessionId
				}
			},
			params:{'deviceId':"2"}
		});

        /**
         * 传输模块配置
         */
        $stateProvider.state('bcSendPlatform',{
            views: {
                'panel': {
                    controller : 'bcSendPlatformManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcSendPlatform/main.html?sessionId='+sessionId
                }
            }
        });
        
        /**
         * 传输策略配置
         */
        $stateProvider.state('bcSendPlatformDataStrategy',{
            views: {
                'panel': {
                    controller : 'bcSendPlatformDataStrategyManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcSendPlatformDataStrategy/main.html?sessionId='+sessionId
                }
            },
            params:{'platformId':"2"}
        });

        /**
         * 212国标传输配置
         */
        $stateProvider.state('bcParamMap212',{
            views: {
                'panel': {
                    controller : 'bcParamMap212ManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcParamMap212/main.html?sessionId='+sessionId
                }
            }
        });
        /**
         * access数据库配置
         */
        $stateProvider.state('bcAccessTable',{
            views: {
                'panel': {
                    controller : 'bcAccessTableManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcAccessTable/main.html?sessionId='+sessionId
                }
            }
        });
        /**
         * access参数配置
         */
        $stateProvider.state('bcAccessParam',{
            views: {
                'panel': {
                    controller : 'bcAccessParamManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcAccessParam/main.html?sessionId='+sessionId
                }
            }
        });

        /**
		 * plc点位配置
         */
        $stateProvider.state('bcPlcPointConfig',{
            views: {
                'panel': {
                    controller : 'bcPlcPointConfigManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bcPlcPointConfig/list.html?sessionId='+sessionId
                }
            }
        });

        /**
		 * plc控制
         */
        $stateProvider.state('bdPlcControl',{
            views: {
                'panel': {
                    controller : 'bdPlcControlManagerController',
                    templateUrl: getContextPath() + '/webResource/view/sw/bdPlcControl/main.html?sessionId='+sessionId
                },
                'message': {
                    controller : 'bdPlcDetailNavController',
                    module:'water.apps.sw.bdPlcControl.bdPlcDetailNav',
                    templateUrl: getContextPath()+ '/webResource/view/sw/bdPlcControl/plcDetailNav.html?sessionId='+sessionId
                }
            }
        })
			/**
			 * 设备详情
			 */
			.state('bdPlcControl.deviceDetail',{
        	params:{data:null},
			controller : 'bdPlcDeviceDetailController',
			module:'water.apps.sw.bdPlcControl.device.bdPlcDeviceDetail',
			templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/device/plcDeviceDetail.html?sessionId='+sessionId
        })
			/**
             * 设备报警
             */
			.state('bdPlcControl.deviceAlarm',{
            params:{data:null},
            controller : 'bdPlcDeviceAlarmController',
            module:'water.apps.sw.bdPlcControl.device.bdPlcDeviceAlarm',
            templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/device/plcDeviceAlarm.html?sessionId='+sessionId
        })
			/**
             * 设备控制
             */
			.state('bdPlcControl.deviceControl',{
            params:{data:null},
            controller : 'bdPlcDeviceControlController',
            module:'water.apps.sw.bdPlcControl.device.bdPlcDeviceControl',
            templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/device/plcDeviceControl.html?sessionId='+sessionId
        })
			/**
             *系统详情
             */
			.state('bdPlcControl.systemDetail',{
            params:{data:null},
            controller : 'bdPlcSystemDetailController',
            module:'water.apps.sw.bdPlcControl.system.bdPlcSystemDetail',
            templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/system/plcSystemDetail.html?sessionId='+sessionId
        })
			/**
             * 系统报警
             */
			.state('bdPlcControl.systemAlarm',{
            params:{data:null},
            controller : 'bdPlcSystemAlarmController',
            module:'water.apps.sw.bdPlcControl.system.bdPlcSystemAlarm',
            templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/system/plcSystemAlarm.html?sessionId='+sessionId
        })
			/**
             * 系统控制
			 */
			.state('bdPlcControl.systemControl',{
            params:{data:null},
            controller : 'bdPlcSystemControlController',
            module:'water.apps.sw.bdPlcControl.system.bdPlcSystemControl',
            templateUrl: getContextPath()+'/webResource/view/sw/bdPlcControl/system/plcSystemControl.html?sessionId='+sessionId
        });
	});
	
	
	wdp.config(function($httpProvider) {
	    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
	    
	    $httpProvider.defaults.transformRequest = [function(data) {
	        /**
	         * The workhorse; converts an object to x-www-form-urlencoded serialization.
	         * @param {Object} obj
	         * @return {String}
	         */
	        var param = function(obj) {
	            var query = '';
	            var name, value, fullSubName, subName, subValue, innerObj, i;
	 
	            if(obj){
	            	obj.sessionId = $("#_sessionid").html();
	            }
	            
	            for (name in obj) {
	                value = obj[name];
	 
	                if (value instanceof Array) {
	                    for (i = 0; i < value.length; ++i) {
	                        subValue = value[i];
	                        fullSubName = name + '[' + i + ']';
	                        innerObj = {};
	                        innerObj[fullSubName] = subValue;
	                        query += param(innerObj) + '&';
	                    }
	                } else if (value instanceof Object) {
	                    for (subName in value) {
	                        subValue = value[subName];
	                        fullSubName = name + '[' + subName + ']';
	                        innerObj = {};
	                        innerObj[fullSubName] = subValue;
	                        query += param(innerObj) + '&';
	                    }
	                } else if (value !== undefined && value !== null) {
	                    query += encodeURIComponent(name) + '='
	                            + encodeURIComponent(value) + '&';
	                }
	            }
	 
	            return query.length ? query.substr(0, query.length - 1) : query;
	        };
	        return angular.isObject(data) && String(data) !== '[object File]'
	                ? param(data)
	                : data;
	    }];
	});
	/**
	* 定义WDP默认控制器
	*/
	wdp.controller('wdpCtrl', ['$rootScope', '$scope','$state','$CommonInit','$nav','$systemService',
		function($rootScope, $scope,$state,$CommonInit,$nav,$systemService){
			
			$CommonInit.init($rootScope);
			$state.go("homePage");
			
			window.Map_H = $(window).height() - 10;
			window.Map_W = $(window).width();
			
			$rootScope.Map_H = $(window).height() - 10;
			$rootScope.Map_W = $(window).width();
			
			$scope.onRestartClick = function(){
				$systemService.restart();
			}

            $systemService.getVersion().then(function (value) {
                $scope.versrion = value[0];
			});
		}
	]);
}(window, angular));